package com.heima.skydemo.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.skydemo.auto.AutoFillCreate;
import com.heima.skydemo.auto.AutoFillUpdate;
import com.heima.skydemo.domin.common.PageResult;
import com.heima.skydemo.domin.common.Result;
import com.heima.skydemo.domin.dto.SetmealDTO;
import com.heima.skydemo.domin.dto.SetmealDishs;
import com.heima.skydemo.domin.dto.SetmealQuerry;
import com.heima.skydemo.domin.vo.SetmealVo;
import com.heima.skydemo.domin.vo.StetmealDishVo;
import com.heima.skydemo.entity.Setmeal;
import com.heima.skydemo.entity.SetmealDish;
import com.heima.skydemo.mapper.SetmealMapper;
import com.heima.skydemo.service.ISetmealDishService;
import com.heima.skydemo.service.ISetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-04-15
 */
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private ISetmealService setmealService;
    @Autowired
    private ISetmealDishService setmealDishService;
    @Autowired
    private SetmealMapper setmealMapper;
    @PutMapping("")
    @Transactional
    @AutoFillUpdate
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐：{}",setmealDTO);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealService.updateById(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes){
            setmealDishService.updateById(setmealDish);
        }
        return Result.success();
    }
    @GetMapping("/page")
    public Result<PageResult> page(SetmealQuerry setmealQuerry){
        log.info("分页查询：{}",setmealQuerry);
        PageResult pageResult = setmealService.pageQuerry(setmealQuerry);
        return Result.success(pageResult);
    }
    @PostMapping("")
    @AutoFillCreate
    @AutoFillUpdate
    public Result save(@RequestBody SetmealDTO setmealdto){
        log.info("新增套餐：{}",setmealdto);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealdto,setmeal);
        setmealService.save(setmeal);
        List<SetmealDish> setmealDishes = setmealdto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes){
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishService.save(setmealDish);
        }
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result<StetmealDishVo> query(@PathVariable Integer id){
        log.info("查询套餐：{}",id);
        Setmeal setmeal = setmealService.getById(id);
       List<SetmealDish> setmealDish = setmealDishService.list(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId,id));
        StetmealDishVo stetmealDishVo = new StetmealDishVo();
        BeanUtils.copyProperties(setmeal,stetmealDishVo);
        stetmealDishVo.setSetmealDishes(setmealDish);
        return Result.success(stetmealDishVo);
    }
    @PostMapping("/status/{status}")
    public Result status(@PathVariable("status")Integer status,Long id){
        log.info("修改套餐状态：{}",status);
        setmealMapper.updateStatus(status,id);
        return Result.success();
    }
    @DeleteMapping("")
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除套餐：{}",ids);
        setmealService.removeBatchByIds(ids);
        return Result.success();
    }





}
